<?php
namespace PublicNo\Controller;
use Common\Controller\CommonController;

//公众号会员控制器
class PublicNoMemberController extends CommonController{
	protected $_public_no;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		$this->_auth();
	
		$this->_moduleid = 110;
		$this->_public_no = new \PublicNo\Model\PublicNoModel();
		$this->_friend = new \Common\Model\FriendModel();
		$this->_cust = new \Common\Model\CustModel();
	}
	
	//获得关注公众号的所有会员列表
	public function memberListAction(){
		$publicnoid = (int)I('request.publicnoid');
		$pageIndex = (int)I('request.pageIndex');
		$pageSize = (int)I('request.pageSize');
		$keywords = I('request.keywords');
		$this->_memberList($publicnoid , $keywords , $pageIndex , $pageSize);
	}
	
	private function _memberList($publicnoid , $keywords , $pageIndex , $pageSize){
		if(empty($publicnoid)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$publicNoInfo = $this->_public_no->getPublicNoInfoById($publicnoid);
		if($publicNoInfo['custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		
		$data = array();
		
		if(!empty($keywords)){
			$arr['searchKey'] = $keywords;
			$ret = $this->_cust->getCustList($arr);
			if(!empty($ret['data'])){
				foreach($ret['data'] as &$cust){
					$data['tcustid'][] = $cust['custid'];
				}
			}
		}
		
		if($pageIndex)$pageIndex--;
		$data['pageIndex'] = $pageIndex;
		$data['pageSize'] = $pageSize;
		$data['custid'] = $this->_custInfo['custid'];
		$friendList = $this->_friend->getFriendList($data);
		if(!empty($friendList['data'])){
			foreach($friendList['data'] as &$friend){
				$custInfo = $this->_getCustInfo($friend['custid']);
				if(empty($friend['nickname'])) $friend['nickname'] = $custInfo['nickname'];
				$friend['realname'] = $custInfo['realname'];
				$friend['pinyinname'] = $custInfo['pinyinname'];
				$friend['sex'] = $custInfo['sex'];
				$friend['birthday'] = $custInfo['birthday'];
				$friend['age'] = $custInfo['age'];
				$friend['mobile'] = $custInfo['mobile'];
				$friend['email'] = $custInfo['email'];
				$friend['area'] = $custInfo['area'];
			}
		}
		
		$this->data = $friendList;
		$this->outPut();
	}
	
	
	//获得关注公众号的会员的详细信息
	public function memberInfoAction(){
		$id = (int)I('request.id');
		$this->_memberInfo($id);
	}
	
	private function _memberInfo($id){
		if(empty($id)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$friendInfo = $this->_friend->getFriendById($id);
		if($friendInfo['_custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$custInfo = $this->_getCustInfo($friendInfo['custid']);
		if(!empty($custInfo)){
			$friendInfo['nickname'] = $custInfo['nickname'];
			$friendInfo['realname'] = $custInfo['realname'];
			$friendInfo['pinyinname'] = $custInfo['pinyinname'];
			$friendInfo['custtype'] = $custInfo['custtype'];
			$friendInfo['sex'] = $custInfo['sex'];
			$friendInfo['birthday'] = $custInfo['birthday'];
			$friendInfo['age'] = $custInfo['age'];
			$friendInfo['mobile'] = $custInfo['mobile'];
			$friendInfo['email'] = $custInfo['email'];
			$friendInfo['area'] = $custInfo['area'];
		}
		
		$this->data = $friendInfo;
		$this->outPut();
	}
	
	//开启/关闭会员状态
	public function opMemberAction(){
		$id = (int)I('request.id');
		$status = (int)I('request.status');
		$this->_opMember($id,$status);
	}
	
	private function _opMember($id,$status){
		if(empty($id)){
			$this->status = 1000;
			$this->msg = L('STATUS_1000');
			$this->outPut();
		}
		
		$friendInfo = $this->_friend->getFriendById($id);
		if($friendInfo['_custid'] != $this->_custInfo['custid']){
			$this->status = 1001;
			$this->msg = L('STATUS_1001');
			$this->outPut();
		}
		
		$data['id'] = $id;
		$data['status'] = $status;
		$this->_friend->updateFriendById($data);
		
		$this->outPut();
	}
	
	
}